package cn.gaozhong.ymcc.service.impl;

import cn.gaozhong.ymcc.constarts.CommonConstants;
import cn.gaozhong.ymcc.domain.Login;
import cn.gaozhong.ymcc.domain.User;
import cn.gaozhong.ymcc.domain.UserAccount;
import cn.gaozhong.ymcc.domain.UserBaseInfo;
import cn.gaozhong.ymcc.dto.RedisRegisterDto;
import cn.gaozhong.ymcc.dto.RegisterDto;
import cn.gaozhong.ymcc.feign.LoginFeignClient;
import cn.gaozhong.ymcc.mapper.UserMapper;
import cn.gaozhong.ymcc.result.JSONResult;
import cn.gaozhong.ymcc.service.IUserAccountService;
import cn.gaozhong.ymcc.service.IUserBaseInfoService;
import cn.gaozhong.ymcc.service.IUserService;
import cn.gaozhong.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author GZ.gaozhong
 * @since 2022-07-15
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private IUserAccountService userAccountService;
    @Autowired
    private IUserBaseInfoService userBaseInfoService;
    @Autowired
    private LoginFeignClient loginFeignClient;

    @Override
    @GlobalTransactional
    public void register(RegisterDto dto) {
        String key = String.format(CommonConstants.REGISTER_PHONE_PRIFIX, dto.getMobile());
        //2.验证码是否正确
        Object o = redisTemplate.opsForValue().get(key);
        AssertUtil.isNotNull(o, "短信验证码过期");
        RedisRegisterDto redisRegisterDto = (RedisRegisterDto) o;
        AssertUtil.isEquals(redisRegisterDto.getCode(),dto.getSmsCode(),"验证码输入错误");
        //3.手机号不能被注册
        User user = selectUserByPhone(dto.getMobile());
        //防御型编程
        AssertUtil.isNull(user,"用户名已存在");
//        ==保存
//         保存t_login
//           。在service-uaa编写controller接口，调用service保存t_login数据
//           。为service-uaa的controller编写Feign，给service-user调用
        JSONResult jsonResult = saveLogin(dto);
        AssertUtil.isTrue(jsonResult.isSuccess(),jsonResult.getMessage());
        //login保存成功了
        AssertUtil.isNotNull(jsonResult.getData(),"登录信息保存失败！");
        Long loginId = Long.valueOf(jsonResult.getData().toString());
//         保存user相关
//           调用对应的service保存数据即可 t_user  t_user_base_info  t_user_account
        user = saveUser(loginId, dto.getMobile());
        AssertUtil.isNotNull(user.getId(),"用户保存失败！！");
        //保存userBaseInfo
        initUserInfo(user.getId(),dto.getRegChannel());
        //保存userAccount
        initUserAccount(user.getId());
    }

    private void initUserAccount(Long userId) {
        UserAccount account = new UserAccount();
        account.setId(userId);
        account.setCreateTime(new Date().getTime());
        userAccountService.insert(account);
    }

    private void initUserInfo(Long userId,Integer regChannel) {
        UserBaseInfo info = new UserBaseInfo();
        info.setId(userId);
        info.setCreateTime(new Date().getTime());
        info.setRegChannel(regChannel);//注册渠道
        userBaseInfoService.insert(info);
    }

    private User saveUser(Long loginId,String mobile) {
        User user = new User();
        user.setLoginId(loginId);
        user.setPhone(mobile);
        user.setNickName(mobile);
        user.setCreateTime(new Date().getTime());
        insert(user);
        return user;
    }

    private JSONResult saveLogin(RegisterDto dto) {
        Login login = new Login();
        login.setUsername(dto.getMobile());
        login.setPassword(dto.getPassword());
        login.setType(Login.TYPE_USER);
        return loginFeignClient.save(login);
    }

    private User selectUserByPhone(String mobile) {
        Wrapper<User> tWrapper = new EntityWrapper();
        tWrapper.eq("phone",mobile);  // where phone = 123123
        return selectOne(tWrapper);
    }
}

